using System;
using System.Collections.Generic;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a mathematical variable in the form ax^y. 
    /// </summary>
    [Serializable]
    public class Variable
    {
        /// <summary>
        /// The coefficient of the variable.
        /// </summary>
        private double coefficient;

        /// <summary>
        /// The exponent of the variable.
        /// </summary>
        private double exponent;

        /// <summary>
        /// The name of the variable.
        /// </summary>
        private string variableName;

        /// <summary>
        /// Initializes a new instance of the <see cref="Variable"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the variable.</param>
        public Variable(double coefficient)
        {
            this.coefficient = coefficient;
            this.exponent = 1;
            this.variableName = "x";
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Variable"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the variable.</param>
        /// <param name="exponent">The exponent of the variable.</param>
        public Variable(double coefficient, double exponent)
        {
            this.coefficient = coefficient;
            this.exponent = exponent;
            this.variableName = "x";
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Variable"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the variable.</param>
        /// <param name="exponent">The exponent of the variable.</param>
        /// <param name="variableName">The name of the variable.</param>
        public Variable(double coefficient, double exponent, string variableName)
        {
            if (variableName == (string) null)
            {
                throw new ArgumentNullException("variableName");
            }

            this.coefficient = coefficient;
            this.exponent = exponent;
            this.variableName = variableName;
        }

        /// <summary>
        /// Gets or sets the coefficient of the variable.
        /// </summary>
        /// <value>The coefficient of the variable.</value>
        public double Coefficient
        {
            get { return coefficient; }
            set { coefficient = value; }
        }

        /// <summary>
        /// Gets or sets the exponent of the variable.
        /// </summary>
        /// <value>The exponent of the variable.</value>
        public double Exponent
        {
            get { return exponent; }
            set { exponent = value; }
        }

        /// <summary>
        /// Gets or sets the name of the variable.
        /// </summary>
        /// <value>The name of the variable.</value>
        public string VariableName
        {
            get { return variableName; }
            set { variableName = value; }
        }

        /// <summary>
        /// Computes the variable with a specified value for x.
        /// </summary>
        /// <param name="var">The value of x.</param>
        /// <returns>The computed result of the variable.</returns>
        public double GetResult(double var)
        {
            return this.coefficient * Math.Pow(var, this.exponent);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first variable.</param>
        /// <param name="b">The second variable.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Variable a, Variable b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.Coefficient == b.Coefficient) && (a.Exponent == b.Exponent) &&
                    (a.VariableName.Equals(b.VariableName)));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first variable.</param>
        /// <param name="b">The second variable.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Variable a, Variable b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first variable.</param>
        /// <param name="b">The second variable.</param>
        /// <returns>The result of the operator.</returns>
        public static Variable operator +(Variable a, Variable b)
        {
            if (a == (Variable) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Variable) null)
            {
                throw new ArgumentNullException("b");
            }

            if ((a.Exponent != b.Exponent) || (!a.VariableName.Equals(b.VariableName)))
            {
                throw new IllegalArithmeticException("The variable names and the exponents have to be even.");
            }

            return new Variable(a.Coefficient + b.Coefficient, a.exponent, a.VariableName);
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first variable.</param>
        /// <param name="b">The second variable.</param>
        /// <returns>The result of the operator.</returns>
        public static Variable operator -(Variable a, Variable b)
        {
            if (a == (Variable) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Variable) null)
            {
                throw new ArgumentNullException("b");
            }

            if ((a.Exponent != b.Exponent) || (!a.VariableName.Equals(b.VariableName)))
            {
                throw new IllegalArithmeticException("The variable names and the exponents have to be even.");
            }

            return new Variable(a.Coefficient - b.Coefficient, a.exponent, a.VariableName);
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first variable.</param>
        /// <param name="b">The second variable.</param>
        /// <returns>The result of the operator.</returns>
        public static Variable operator *(Variable a, Variable b)
        {
            if (a == (Variable) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (Variable) null)
            {
                throw new ArgumentNullException("b");
            }

            if (!a.VariableName.Equals(b.VariableName))
            {
                throw new IllegalArithmeticException("The variable names have to be even.");
            }

            return new Variable(a.Coefficient * b.Coefficient, a.exponent + b.exponent, a.VariableName);
        }

        /// <summary>
        /// Compares the current instance of a variable with another.
        /// </summary>
        /// <param name="compare">The compare.</param>
        /// <returns>True if the compared variables are even otherwise, false.</returns>
        public bool CompareTo(Variable compare)
        {
            if (compare == (Variable) null)
            {
                throw new ArgumentNullException("compare");
            }

            return this == compare;
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public Variable Copy()
        {
            return new Variable(this.coefficient, this.exponent, this.variableName);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.coefficient.ToString() + this.variableName.ToString() + "^" + this.exponent.ToString();
        }
    }
}